﻿using UnityEngine;
using System.Collections;

public class WeaponsScript : Photon.MonoBehaviour 
{
	public float BulletForce = 800.0f;
	private float RateOfFire = 2.0f;
	
	
	public Transform bulletsSpawnPoint = null;
//	public CFS_Bullet bulletPrefab;
	
	private float nextFireTime = 0.0f;
	
	[SerializeField] GameObject[] bulletPrefabs = null;
	
	private int currentBullet = 0;

	public int currentWeapon = 0;
	public int lastWeapon = 0;
	
	

	
//	 Update is called once per frame
	void Update () 
	{
		if(lastWeapon != currentWeapon)
			WeaponBehavior();
		
		if (Input.GetButton ("Fire1") && Time.time > nextFireTime) 
		{
	        nextFireTime = Time.time + RateOfFire;
	        photonView.RPC("Shoot", PhotonTargets.All);
    	}
	}
	
	void WeaponBehavior()
	{
		switch(currentWeapon)
		{
			case 0:
			{
				BulletForce = 800.0f;
				RateOfFire	= 0.5f;
			
				currentBullet = 0;
			
				break;
			}
			
			case 1:
			{
				BulletForce = 600.0f;
				RateOfFire	= 0.16f;
			
				currentBullet = 0;
				
				break;
			}
			
			case 2:
			{
				BulletForce = 1200.0f;
				RateOfFire	= 2.0f;
			
				currentBullet = 1;
				
				break;
			}
			
			case 3:
			{
				BulletForce = 400.0f;
				RateOfFire	= 0.5f;
				
				currentBullet = 2;
				
				break;
			}
			
			case 4:
			{
				BulletForce = 800.0f;
				RateOfFire	= 2.0f;
				
				currentBullet = 3;
				
				break;
			}
		}
		
		lastWeapon = currentWeapon;
	}
	
	
	[RPC]
	void Shoot()
	{
		GameObject newBullet = Instantiate(bulletPrefabs[currentBullet], bulletsSpawnPoint.position, bulletsSpawnPoint.rotation) as GameObject;
			
		newBullet.rigidbody.AddForce(bulletsSpawnPoint.forward.normalized *  BulletForce);
	}
}
